import "../pages/Home.vue";


import {createRouter, createWebHistory} from "vue-router";
import {useSongStore} from "./pinia.ts";
import _ from "lodash";
const ForgetEditPassword = ()=> import("../pages/ForgetEditPassword.vue");
const App = () => import("../App.vue");
const Login = () => import('../pages/Login.vue')
const Home = () => import("../pages/Home.vue")
const Message = () => import("../pages/Msg.vue")
const User = () => import("../pages/User.vue")
const Register = () => import('../pages/Register.vue')
const ImproveInfo = () => import('../pages/ImproveInfo.vue')
const Add = () => import('../pages/Add.vue')
const AddOne = () => import('../pages/add/Add-One.vue')
const AddTwo = () => import('../pages/add/Add-Two.vue')
const SongInfo = () => import('../pages/SongInfo.vue')
const Favorite = () => import('../pages/Favorite.vue')
const Playlist = () => import('../pages/Playlist.vue')
const UpdateLyric = () => import('../pages/UpdateLyric.vue')
const Search = () => import('../pages/Search.vue')
const EditUser = () => import('../pages/EditUser.vue')
const Edit = () => import('../pages/Edit.vue')
const EditPassword = () => import('../pages/EditPassword.vue')
const ForgetPassword = () => import('../pages/ForgetPassword.vue')


//定义一些路由
const routes = [
    {
        path: '/',
        component: App,
        redirect: '/home'
    },
    {
        path: '/home',
        component: Home
    },
    {
        path: '/msg',
        component: Message
    },
    {
        path: '/user',
        component: User,
    },
    {
        path: '/login',
        component: Login
    },
    {
        path: '/forgetPassword',
        component: ForgetPassword,
    },
    {
        path: '/register',
        component: Register,
    },
    {
        path: '/improveInfo',
        component: ImproveInfo,
    },
    {
        path: '/add',
        component: Add,
        redirect: '/add/add-one',
        children:[
            {
                path : 'add-one',
                component: AddOne,
            },
            {
                path: 'add-two',
                component: AddTwo,
            }
        ]
    },
    {
        path: '/songInfo/:songId',
        component: SongInfo,
        props: true,
    },
    {
        path: '/favorite/:userId',
        component: Favorite,
        props: true,
    },
    {
        path: '/playlist/:playlistId',
        component: Playlist,
        props: true,
    },
    {
        path: '/updateLyric/:songId',
        component: UpdateLyric,
        props: true,
    },
    {
        path: '/search/:keyword',
        component: Search,
        props: true,
    },
    {
        path: '/edit',
        component: Edit,
        props: true,
    },
    {
        path: '/editUser',
        component: EditUser,
        props: true,
    },
    {
        path: '/editPassword',
        component: EditPassword,
        props: true,
    },
    {
        path: '/forgetEditPassword',
        component: ForgetEditPassword,
        props: true,
    },

]


const router = createRouter({
    //  内部提供了 history 模式的实现。
    history: createWebHistory(),
    // @ts-ignore
    routes, // `routes: routes` 的缩写
    scrollBehavior () {
        // return 期望滚动到哪个的位置
        return { top: 0 }
    }
})


//定义一些需要登录验证和拦截的路由地址
let loginAfter = [
    '/user',
    '/add',
    '/favorite',
    '/editUser',
    '/editPassword',
    '/edit',
    '/updateLyric'
]


//创建一个导航守卫
// @ts-ignore
router.beforeEach((to, from) => {
    //用于验证用户是否登录，并且跳转链接是否在拦截列表里
    let isAuthenticated = sessionStorage.getItem("isAuthenticated");
    if (isAuthenticated != 'true'){
        let i = false
        loginAfter.forEach((value)=>{
            if (_.startsWith(to.fullPath, value)) {
                i =  true
                return
            }
        })
        if(i){
            //用户未登录
            console.log("用户未登录")
            return '/login';
        }
    } else if (isAuthenticated == 'true') {
        //用户登录
        if (to.fullPath == '/login') {
            return '/user'
        } else if (to.fullPath == '/add/add-two') {
            let url = useSongStore().song.url;
            if (url == null) {
                return '/add/add-one'
            }
        }
    }
    return true
})


export default router